<cfcomponent>

	<cfscript>
		this.name = "WMS";
		this.clientManagement = false;
		this.loginStorage = "cookie";
		this.applicationTimeout = CreateTimeSpan(100,0,0,0);
		// this.mappings["/assets"] = "C:\inetpub\assets";
		// this.mappings["/validateThis"] = "C:\inetpub\assets\validateThis";
		// StructInsert(this.mappings, "/validateThis", "C:\inetpub\assets\validateThis");
	</cfscript>
	
	<cffunction name="onApplicationStart">
		<cfscript>
			if (cgi.HTTP_HOST eq "calgrants"){
				application.mode = "dev";
				
			} else {
				application.mode = "prod";
			}
			application.datasource = "wms";
			application.pathToApplication = "/applications/workshops";
			application.pathToJQuery = "/assets/jquery/jquery.min.js";
			application.pathToJQueryUI = "/assets/jquery/jquery-ui.min.js";
			application.pathToKendo = "/assets/kendoui/js/kendo.all.min.js";
			application.pathToKendoDefaultCSS = "/assets/kendoui/styles/kendo.default.min.css";
			application.pathToKendoCommonCSS = "/assets/kendoui/styles/kendo.common.min.css";
			application.pathToBootstrap = "/assets/bootstrap/js/bootstrap.min.js";
			application.pathToBootstrapCSS = "/assets/bootstrap/css/bootstrap.min.css";
			//application.pathToBootstrapCSS = "/assets/bootstrap/css/bootstrap-combined.no-icons.min.css";
			application.pathToFontAwesomeCSS = "/assets/font-awesome/css/font-awesome.min.css";
			application.pathToComponents = "assets.calgrants.com";
			application.pathToBO = application.pathToComponents & ".bo";
			application.pathToServices = application.pathToComponents & ".service";
			application.pathToGateways = application.pathToComponents & ".gateway";
			application.pathToUI = "/assets/calgrants/ui";
			application.pathToTemplates = "/assets/calgrants/ui/templates";
			application.pathToTemp = "/assets/calgrants/temp/";
			application.tempDirectory = ExpandPath(GetDirectoryFromPath(application.pathToTemp));
			application.apiUrl = "/assets/calgrants/com/API.cfc";
			application.wrapperUrl = "/wseval/";
			application.personality = {};
			application.personality.name = "Cash for College";
			application.personality.email = "cashforcollege@csac.ca.gov";
			application.personality.bcc = "cash4collegeca@gmail.com";
			application.personality.phone = "916-464-6410";
			application.developer = {};
			application.developer.name = "Syntric";
			application.developer.email = "ken@syntric.com";
			application.developer.phone = "916-776-6650";
			application.fulfillment = {};
			application.fulfillment.name = "Syntric";
			application.fulfillment.email = "ken@syntric.com";
			application.payment = {};
			application.payment.name = "Los Angeles Area Chamber of Commerce";
			application.payment.email = "cashforcollege@lachamber.com";
			application.currentProgramYear = "2014-2015";
			application.initDateTime = now();
            initValidator();
		</cfscript>
	</cffunction>
	
	<cffunction name="onRequestStart">
		<cfif cgi.HTTPS eq "off">
			<cflocation url="https://#cgi.SERVER_NAME##cgi.SCRIPT_NAME#" addtoken="no">
		</cfif>
		<cfset request.alerts = ArrayNew(1) />
		<cfif IsDefined("initApp")>
			<cflogout />
			<cfscript>
				onApplicationStart();
			</cfscript>
		</cfif>
		<cfif IsDefined("initValidator")>
			<cfscript>
				initValidator();
			</cfscript>
		</cfif>
		<cflock scope="application" timeout="20" type="readonly">
			<cfset StructAppend(request, application) />
		</cflock>
		<cfif IsDefined("logout")>
			<cflogout />
		</cfif>
		<cfscript>
			var storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
		</cfscript>
		<cflogin>
			<cfif IsDefined("cflogin") and structKeyExists(cflogin, "name") and structKeyExists(cflogin, "password") and isValidHost()>
				<cfscript>
					var authUser = storedProcedureGateway.call(sp = "authUser", User_ID = "", Email = cflogin.name, Password = cflogin.password, Program_Year = request.currentProgramYear);
				</cfscript>
				<cfif authUser.RecordCount eq 1 AND authUser.Approved AND NOT authUser.Archived>
					<cfif authUser.Role eq "Organizer" or authUser.Role eq "Site Contact">
						<cfloginuser name="#authUser.User_ID#" password="" roles="#authUser.Role#" />
					<cfelse>
						<cflocation url="/applications/admin/?logout" addtoken="no" />
					</cfif>
				<cfelseif authUser.RecordCount eq 1 AND NOT authUser.Approved>
					<cfset ArrayAppend(request.alerts, "Account Inactive\n\nContact your organizer, regional admin or #request.personality.name# at #request.personality.email# to approve your account.") />
				<cfelseif authUser.RecordCount eq 0>
					<cfset ArrayAppend(request.alerts, "Login Failed\n\nCheck your Email and Password. Contact your organizer, regional admin or #request.personality.name# at #request.personality.email# for assistance.") />
				<cfelseif authUser.RecordCount gt 1>
					<cfset ArrayAppend(request.alerts, "Duplicate Accounts\n\nMore than one account exists for #cflogin.name#.  Contact your organizer, regional admin or #request.personality.name# at #request.personality.email# for assistance.") />
				</cfif>
			</cfif>
		</cflogin>
		<cfif NOT IsUserLoggedIn()>
			<cfinclude template="login.cfm" />
			<cfabort>
		<cfelseif NOT IsUserInRole("system")>
			<cfscript>
				var authUser = storedProcedureGateway.call(sp = "authUser", User_ID = GetAuthUser(), Program_Year = request.currentProgramYear);
			</cfscript>
			<cfif authUser.RecordCount eq 1 AND authUser.Approved AND NOT authUser.Archived AND NOT authUser.Organization_Deleted AND NOT authUser.Organization_Archived>
				<cfif GetUserRoles() neq authUser.Role>
					<cfloginuser name="#authUser.User_ID#" password="" roles="#authUser.Role#" />
				</cfif>
				<cfscript>
					request.authUser.lastVisit = Now();
					request.authUser.organizationID = authUser.Organization_ID;
					request.authUser.organization = authUser.Organization;
					request.authUser.paID = authUser.PA_ID;
					request.authUser.oapID = authUser.OAP_ID;
					request.authUser.regionID = authUser.Region_ID;
					request.authUser.userID = authUser.User_ID;
					request.authUser.fullName = authUser.First_Name & " " & authUser.Last_Name;
					request.authUser.email = authUser.Email;
					request.authUser.role = authUser.Role;
				</cfscript>
			<cfelseif authUser.RecordCount eq 1 AND NOT authUser.Approved>
				<cfset ArrayAppend(request.alerts, "Account Inactive\n\nContact your organizer, regional admin or #request.personality.name# at #request.personality.email# for assistance.") />
				<cfinclude template="login.cfm" />
				<cfabort>
			<cfelseif authUser.RecordCount eq 0>
				<cfset ArrayAppend(request.alerts, "Login Failed\n\nCheck your Email and Password. Contact your organizer, regional admin or #request.personality.name# at #request.personality.email# for assistance.") />
				<cfinclude template="login.cfm" />
				<cfabort>
			<cfelseif authUser.RecordCount gt 1>
				<cfset ArrayAppend(request.alerts, "Duplicate Accounts\n\nMore than one account exists for #cflogin.name#.  Contact your organizer, regional admin or #request.personality.name# at #request.personality.email# for assistance.") />
				<cfinclude template="login.cfm" />
				<cfabort>
			</cfif>
		</cfif>
	</cffunction>

	<cffunction name="onError">
		<cfargument name="Exception" required="true" />
		<cfargument name="EventName" required="true" />
		<cfif application.mode eq "prod">
			<cfmail to="kenhobbs@gmail.com" from="kenhobbs@gmail.com" subject="Organizer Error" type="html">
				<cfdump var="#arguments.Exception#" expand="yes" label="Exception" />
				<cfdump var="#cgi#" expand="yes" label="CGI" />
				<cfdump var="#form#" expand="yes" label="Form" />
				<cfdump var="#url#" expand="yes" label="URL" />
			</cfmail>
		<cfelse>
			<cfdump var="#arguments.Exception#" expand="yes" label="Exception" />
			<cfdump var="#cgi#" expand="no" label="CGI" />
			<cfdump var="#form#" expand="no" label="Form" />
			<cfdump var="#url#" expand="no" label="URL" />
		</cfif>
		<h1>Ooops</h1>
		<p>The application has encountered an error.  An error message has been sent to the developer.</p>
		<p>If you require immediate assistance with this error, please <a href="mailto:support@syntric.com">contact the developer</a>.</p>
		<p>If you encountered this error while logging into the system, <a href="?logout">follow this link</a> and try again.</p>
		<cfabort>
	</cffunction>
	
	<cffunction name="onRequestEnd">
        <cfsetting showdebugoutput="no">
    </cffunction>
	
	<cffunction name="isValidHost" access="private" returntype="boolean">
		<cfscript>
			var retVal = false;
			if (ListFind(cgi.HTTP_REFERER, "www.calgrants.org", "/") gt 0 or ListFind(cgi.HTTP_REFERER, "calgrants.org", "/") gt 0 or cgi.HTTP_HOST eq "calgrants") {
				retVal = true;
			}
			return retVal;
		</cfscript>
	</cffunction>

    <cffunction name="initValidator">
        <cfscript>
            lock scope="application" timeout="20" type="readonly"
            {
                var validatorConfig = {extraRuleValidatorComponentPaths = "/assets/calgrants/com/validators/", definitionPath = "/assets/calgrants/com/bo/"};
                application.validator = new validateThis.ValidateThis(validatorConfig);
            }
        </cfscript>
    </cffunction>

</cfcomponent>